import requests


class SendMsgToWorkWeiXin:

    def __init__(self):
        self.session = requests.session()

    """
    发送测试数据结果统计
    """
    def send_test_results_msg(self, webhook, desc, cases_total, cases_success_total, cases_fail_total, cases_skipped_total, cases_time_total, cases_report_address):
        """
        :param webhook: 群机器人 webhook 地址
        :param desc: 发送企业微信消息描述
        :param cases_total: 用例总数
        :param cases_success_total: 成功用例数
        :param cases_fail_total: 失败用例数
        :param cases_skipped_total: 跳过用例数
        :param cases_time_total: 仅会话运行时间
        :param cases_report_address: 测试报告链接跳转地址
        :return:
        """
        body = {
            "msgtype": "template_card",
            "template_card": {
                "card_type": "text_notice",
                "main_title": {
                    "title": "自动化测试结果数据统计",
                    "desc": desc
                },
                "horizontal_content_list": [
                    {
                        "keyname": "运行总数",
                        "value": cases_total
                    },
                    {
                        "keyname": "运行成功",
                        "value": cases_success_total
                    },
                    {
                        "keyname": "运行失败",
                        "value": cases_fail_total
                    },
                    {
                        "keyname": "运行跳过",
                        "value": cases_skipped_total
                    },
                    {
                        "keyname": "运行会话耗时",
                        "value": str(cases_time_total) + " sec"
                    },
                    {
                        "keyname": "测试报告地址",
                        "value": "点击访问",
                        "type": 1,
                        "url": cases_report_address
                    }
                ],
                "jump_list": [
                    {
                        "type": 1,
                        "url": cases_report_address,
                        "title": "测试报告地址"
                    }
                ],
                "card_action": {
                    "type": 1,
                    "url": cases_report_address
                }
            }
        }
        response = self.session.post(url=webhook, json=body)
        print("正在发送测试结果通知, 结果如下:")
        print(response.json())

    """
    通知人员
    """
    def send_notifying_officer(self, webhook, notifying_officer):
        """
        :param webhook: 群机器人 webhook 地址
        :param notifying_officer: 通知人员 list列表 例如 ["150xxx", "@all"] 其中手机号为唯一标识 @all 为艾特全体人员
        :return:
        """
        body = {
            "msgtype": "text",
            "text": {
                "mentioned_mobile_list": notifying_officer
            }
        }
        response = self.session.post(url=webhook, json=body)
        print("正在通知企业微信人员, 结果如下:")
        print(response.json())